using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using  Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

using SqlWebAdmin.CSharp;

public partial class QueryDatabase : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void cmdExecute_Click(object sender, EventArgs e)
    {
        if (txtQuery.Text.Trim().Length > 0)
        {
            
                lblError.Text = string.Empty;
                lblError.Visible = false;

				Server smoServer = SmoHelper.GetServer();
                try
                {
                    string databaseName = Request.QueryString["database"];
                    //smoServer.ConnectionContext.DatabaseName = databaseName;
					 Database db = SmoHelper.GetDatabase();
                    

                    DataSet ds = new DataSet();
					try
					{
						ds = db.ExecuteWithResults(txtQuery.Text);
					}
					catch (Microsoft.SqlServer.Management.Common.ExecutionFailureException execEx)
					{
						panResults.Visible = false;
						lblError.Visible = true;
						lblError.Text = string.Format("{0}<br>",HttpUtility.HtmlEncode(SqlWebAdmin.CSharp.Common.ConcatenateExceptions(execEx)));
						return;
					}
					catch (Exception ex)
					{
						panResults.Visible = false;
						lblError.Visible = true;
						lblError.Text = string.Format("{0}<br>",HttpUtility.HtmlEncode(SqlWebAdmin.CSharp.Common.ConcatenateExceptions(ex)));
						return;	
					}

                    if (ds.Tables.Count > 0)
                    {
                        Label label = new Label();
                        label.Text = "<br><br>";
                        panResults.Controls.Add(label);

                        for (int i = 0; i < ds.Tables.Count; i++)
                        {
                            if (i > 0)
                            {
                                label = new Label();
                                label.Text = "<br><br><hr><br><br>";
                                panResults.Controls.Add(label);
                            }

                            DataGrid dataGrid = new DataGrid();
                            dataGrid.CssClass = "TableGrid";
                            dataGrid.HeaderStyle.CssClass = "TableHeader";
                            dataGrid.ItemStyle.CssClass = "TableItem";
                            dataGrid.ItemStyle.Wrap = false;
                            dataGrid.Width = Unit.Percentage(100);
                            dataGrid.EnableViewState = false;

                            dataGrid.PreRender += new EventHandler(dataGrid_PreRender);

                            dataGrid.DataSource = ds.Tables[i];
                            dataGrid.DataBind();

                            panResults.Controls.Add(dataGrid);
                        }
                    }

                }
                catch (SmoException smoEx)
                {
                    lblError.Text = string.Format("{0}<br>",HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx)));
                    lblError.Visible = true;
                }
                catch (System.Exception ex)
                {
                    lblError.Text = string.Format("{0}<br>",HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex)));
                    lblError.Visible = true;
                }

           
        }
        else
        {
            lblError.Text = "You must enter a query.";
            lblError.Visible = true;
            panResults.Visible = false;
        }
    }

    void dataGrid_PreRender(object sender, EventArgs e)
    {
        DataGrid dg = (DataGrid)sender;

        foreach (DataGridItem item in dg.Items)
        {
            foreach (TableCell tc in item.Cells)
            {
                tc.Wrap = chkWrapText.Checked;
                tc.Text = HttpUtility.HtmlEncode(tc.Text);
                tc.Text = tc.Text.Replace("&amp;nbsp;", "&nbsp;");
            }
        }
    }

    protected void cmdLoad_Click(object sender, EventArgs e)
    {
        System.IO.StreamReader stream = new System.IO.StreamReader(hifFileUpload.PostedFile.InputStream);

        txtQuery.Text = stream.ReadToEnd();

    }

    protected void cmdSave_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("Content-Type:", "text/plain");
        Response.AddHeader("Content-Disposition", "attachment; filename=query.sql");
        Response.Write(txtQuery.Text);
        Response.End();
    }
}
